<!-- 手机验证码验证 -->
<template>
  <button @click="getVerifyCode" :class="(bol&&text=='获取验证码')?'curr':''">{{text}}</button>
</template>

<script>
import { _validateImgCode,_sendMessage } from "../axios/index.js";
export default {
data() {
return {
    text:"获取验证码",
    timer:""
};
},
name:'yzm',
computed: {},
watch: {},
methods: {
    async getVerifyCode() {
      let _self = this;
      if(!_self.bol||_self.text!='获取验证码')return;
      const data = {
        tcode:_self.imgCode
      };
      const res = await _validateImgCode(data,{user:'loading'});
      if (res.data.code === 400) {
        // 错误重新获取验证码图片
        _self.$Message.error({
          content:res.data.msg,
          duration:1
        });
        _self.$emit("update:resetImg",!_self.resetImg);
      } else if (res.data.code === 200) {
        // 成功则关闭验证码弹窗并发送短信
        _self.sendMessage();
      }
    },
    async sendMessage() {
      let _self = this;
      const data = {
        mobile:_self.mobile,
        type:_self.type
      };
      const res = await _sendMessage(data,{user:'loading'});
      if (res.data.code === 200) {
         _self.$Message.success({
          content:res.data.data,
          duration:1
        });
        _self.countDown();
      } else {
        _self.$Message.error({
          content:res.data.msg,
          duration:1
        });
      }
       _self.$emit("update:resetImg",!_self.resetImg);

    },
    countDown() {
      const TIME_COUNT = 60;
      var _self=this;
      if (!_self.timer) {
        _self.text = TIME_COUNT;
        _self.timer = setInterval(() => {
          if (_self.text > 0 && _self.text <= TIME_COUNT) {
            _self.text--;
          } else {
            clearInterval(_self.timer);
            _self.timer = null;
            _self.text="获取验证码";
          }
        },1000)
      }
    },
},
props:["mobile","bol","imgCode","resetImg","type"],//mobile：手机号 ， bol 是否可以点击 ，imgCode：输入的图片验证码， resetImg：重置图片验证码
created() {},
mounted() {},
}
</script>